<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['dictId']"/>
        <a-input v-decorator="['tableId']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名字">
        <a-input placeholder="名字" v-decorator="['name',{rules: [{required: true, message: '请输入名字'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="编号">
                  <a-input placeholder="code" v-decorator="['code',{rules: [{required: true, message: '请输入编号'}]}]"/>
       </a-form-item>
       <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="方法名">
              <a-input placeholder="codeMethod" v-decorator="['codeMethod',{rules: [{required: true, message: '请输入方法名'}]}]"/>
       </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="查询SQL">
        <a-input placeholder="statement" v-decorator="['statement',{rules: [{required: true, message: '请输入查询SQL'}]}]"/>
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
        <a-select  style="width: 100%"  v-decorator="['type', {rules: [{ required: true, message: '请选择多选' }]}]" :allowClear="true">
          <a-select-option v-for="(d,index) in dict_input_typeDict" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="多选">
        <a-select  style="width: 100%"  v-decorator="['multiple', {rules: [{ required: true, message: '请选择多选' }]}]" :allowClear="true">
           <a-select-option v-for="(d,index) in sys_yes_noDictList" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>
<script>
import moment from "moment";
import { dictList,getDictMapKey } from '@/utils/dict'
import { saveGenTableDict,getGenTableDictInfo } from '@/api/gen/tableDict'
import pick from 'lodash.pick'

export default {
  name: 'GenTableDictModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    this.dictSync();
  },
  methods: {
    async dictSync(){
      this.dict_input_typeDict=await dictList('dict_input_type');
      this.sys_yes_noDictList=await dictList('sys_yes_no');
    } ,
    add (query) {
     console.info("query form:"+JSON.stringify(query))
      this.form.resetFields()
        if(query) {
            this.query=query;
            this.mdl = Object.assign(this.mdl,this.query)
        }
      this.edit()
    },
    edit (dictId,query) {
        console.info("query form:"+JSON.stringify(query))
        if(query){
            this.query=query;
        }
      if(dictId){
        getGenTableDictInfo(dictId).then(res => {
          this.mdl = Object.assign(res)
          this.mdl = Object.assign(this.mdl,this.query)

          this.visible = true
          ;
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl,'dictId','tableId','name','statement','type','multiple','code','codeMethod'))
          })
        })
      }else{
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl,'tableId','name','statement','type','multiple','code','codeMethod'))
        })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveGenTableDict(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.success(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>